package com.bhatni.taskmanager.schedule;

import java.util.Calendar;
import java.util.Date;

public class CalendarSchedule extends ScheduleBase {

    int year = -1;
    int month = -1;
    int day = -1;
    int dayType = 5;
    int hour = -1;
    int minute = -1;
    int second = -1;
    int millisecond = -1;
    final int[] fieldtypes = {13, 12, 10,
        this.dayType, 2, 1};

    public int getHour() {
        return this.hour;
    }

    public void setHour(int hour) {
        this.hour = hour;
        if ((hour < 0) || (hour > 24)) {
            this.hour = -1;
        }
    }

    public int getMillisecond() {
        return this.millisecond;
    }

    public void setMillisecond(int millisecond) {
        this.millisecond = millisecond;
    }

    public int getMinute() {
        return this.minute;
    }

    public void setMinute(int minute) {
        this.minute = minute;
    }

    public int getMonth() {
        return this.month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getSecond() {
        return this.second;
    }

    public void setSecond(int second) {
        this.second = second;
    }

    public int getYear() {
        return this.year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getDay() {
        return this.day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    @Override
    public Date getNextSchedule() {
        int[] fieldvalues = {this.second, this.minute, this.hour, this.day,
            this.month, this.year};

        Calendar nextSchedule = Calendar.getInstance();
        Calendar lastSchedule = Calendar.getInstance();
        if (this.lastScheduleTime == null) {
            this.lastScheduleTime = nextSchedule.getTime();
        } else {
            nextSchedule.setTime(this.lastScheduleTime);
            lastSchedule.setTime(this.lastScheduleTime);
        }

        int tmpValue = -1;
        for (int i = this.fieldtypes.length - 1; i >= 0; i--) {
            if (fieldvalues[i] == -1) {
                if (tmpValue == 0) {
                    fieldvalues[i] = tmpValue;
                    nextSchedule.set(this.fieldtypes[i], tmpValue);
                }
            } else {
                nextSchedule.set(this.fieldtypes[i], fieldvalues[i]);
                tmpValue = 0;
            }
        }

        for (int i = 0; (i < this.fieldtypes.length) && (!nextSchedule.after(lastSchedule));
                i++) {
            if (fieldvalues[i] == -1) {
                nextSchedule.add(this.fieldtypes[i], 1);
                if (!nextSchedule.after(lastSchedule)) {
                    nextSchedule.add(this.fieldtypes[i], -1);
                }
            }

        }

        this.lastScheduleTime = nextSchedule.getTime();
        return this.lastScheduleTime;
    }


    public int getDayType() {
        return this.dayType;
    }

    public void setDayType(int dayType) {
        this.dayType = dayType;
    }
}
